package picounit.java.collection;

import java.util.Iterator;

class FilterIterator<T> implements Iterator<T> {
    public FilterIterator(Iterator<T> iterator, Filter<T> filter) {
        this.iterator = iterator;
        this.filter = filter;
    }

    public boolean hasNext() {
        while (iterator.hasNext()) {
            next = iterator.next();

            if (filter.passes(next)) {
                return true;
            }
        }

        next = null;

        return false;
    }

    public T next() {
        return next;
    }

    public void remove() {
        iterator.remove();
    }

    private T next;

    private final Iterator<T> iterator;
    private final Filter<T> filter;
}